package mes.generator;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.junit.Test;

/**
 * <p>
 * 测试生成代码
 * </p>
 *
 * @author 华云龙
 * @date 2018-6-14
 */
public class GeneratorServiceEntityMysql {



    @Test
    public void generateCode() {
        /// 包名
        String packageName = "com.zx.mes.mybatis.hyl";
        /* user -> UserService, 设置成true: user -> IUserService */
//        boolean serviceNameStartWithI = false;
        generateByTables(false, packageName, "t_permission");
    }

    private void generateByTables(boolean serviceNameStartWithI, String packageName, String... tableNames) {
        GlobalConfig config = new GlobalConfig();
        String dbUrl = "jdbc:mysql://10.30.90.41:3306/nacos_upms?useUnicode=true&characterEncoding=UTF-8&useSSL=false";
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.MYSQL)
                .setUrl(dbUrl)
                .setUsername("admin")
                .setPassword("123456")
                .setDriverName("com.mysql.jdbc.Driver");
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig
                .setCapitalMode(true)
                .setEntityLombokModel(true)
//                .setDbColumnUnderline(true)
                .setTablePrefix("t_")
                .setEntityBuilderModel(true)
                .setNaming(NamingStrategy.underline_to_camel)
                .setSuperControllerClass("com.zx.mes.hyl.controller.BaseController")
                .setRestControllerStyle(true) // controller设置为restController
                .setInclude(tableNames);//修改替换成你需要的表名，多个表名传数组
        config.setActiveRecord(true)
                .setBaseResultMap(true)
                .setBaseColumnList(true)
                .setFileOverride(true)
                .setAuthor("华云龙")
                .setEnableCache(false)  /// 取消二级缓存
///             存放目录
                .setOutputDir("d:\\codeGen")
                .setFileOverride(true);
        if (!serviceNameStartWithI) {
            config.setServiceName("%sService");
        }

        AutoGenerator gen = new AutoGenerator();


        gen.setGlobalConfig(config)
                .setDataSource(dataSourceConfig)
                .setStrategy(strategyConfig)
                .setPackageInfo(
                        new PackageConfig()
                                .setParent(packageName)
                                .setController("controller")
                                .setEntity("entity")
                ).execute();
    }

}
